##################################################################
# File : /lua/maui/button.lua
# Description : Button control
# Author : GPG
##################################################################

local Bitmap = import('bitmap.lua').Bitmap
local Dragger = import('dragger.lua').Dragger
local UIUtil = import('/lua/ui/uiutil.lua')

Button = Class(Bitmap) {

    __init = function(self, parent, normal, active, highlight, disabled, selected, clickCue, rolloverCue, frameRate, debugname)
        Bitmap.__init(self, parent, normal)
        self:SetName(debugname or "button")
        self.mNormal = normal
        self.mActive = active
        self.mHighlight = highlight
        self.mDisabled = disabled
        self.mSelected = selected
        self.mMouseOver = false
        self.mClickCue = clickCue
        self.mRolloverCue = rolloverCue
        self.mAltToggledFlag = false
        self.mAltNormal = normal
        self.mAltActive = active
        self.mAltHighlight = highlight
        self.mAltDisabled = disabled
        self.mAltSelected = selected
        self.mFailToActivateCue = nil
        if frameRate then
            self:SetFrameRate(frameRate)
        end
        self:SetLoopPingPongPattern()
        self:Loop(true)
    end,

    SetNewTextures = function(self, normal, active, highlight, disabled, selected)
        self.mNormal = normal
        self.mActive = active
        self.mHighlight = highlight
        self.mDisabled = disabled
        self.mSelected = selected
    end,

    SetUpAltButtons = function(self, normal, active, highlight, disabled, selected)
        self.mAltNormal = normal
        self.mAltActive = active
        self.mAltHighlight = highlight
        self.mAltDisabled = disabled
        self.mAltSelected = selected
    end,
    
    ApplyTextures = function(self)
        if self.mAltToggledFlag then
            if self._isDisabled and self.mAltDisabled then
                self:SetTexture(self.mAltDisabled)
            elseif self._isSelected and self.mAltSelected then
                self:SetTexture(self.mAltSelected)
            elseif self.mAltNormal then
                self:SetTexture(self.mAltNormal)
            end
        else
            if self._isDisabled and self.mDisabled then
                self:SetTexture(self.mDisabled)
            elseif self._isSelected and self.mSelected then
                self:SetTexture(self.mSelected)
            elseif self.mNormal then
                self:SetTexture(self.mNormal)
            end
        end
        self:Play()
    end,

    OnAltToggle = function(self)
        if self.mAltToggledFlag then
            self.mAltToggledFlag = false
            self:SetTexture(self.mActive)
        else
            self.mAltToggledFlag = true
            self:SetTexture(self.mAltActive)
        end
        self:Play()
    end,

    OnDisable = function(self)
        self:ApplyTextures()
    end,

    OnRolloverEvent = function(self, state)
    end,
    
    OnEnable = function(self)
        self:ApplyTextures()
    end,
    
    OnSelect = function(self)
        self:ApplyTextures()
    end,

    HandleEvent = function(self, event)
        local eventHandled = false

        if event.Type == 'MouseEnter' then
            if self.mDragger then
                if not self._isDisabled and not self._isSelected then
                    if self.mAltToggledFlag then
                        self:SetTexture(self.mAltActive)
                    else
                        self:SetTexture(self.mActive)
                    end
                    self:OnRolloverEvent('enter')
                    self:Play()
                end
            else
                if not self._isDisabled and not self._isSelected then
                    if self.mAltToggledFlag then
                        self:SetTexture(self.mAltHighlight)
                    else
                        self:SetTexture(self.mHighlight)
                    end
                    self:OnRolloverEvent('enter')
                    self:Play()
                    if self.mRolloverCue and not self._isDisabled and not self._isSelected then 
                        PlaySound(self.mRolloverCue)
                    end
                end
            end
            self.mMouseOver = true
            eventHandled = true
        elseif event.Type == 'MouseExit' then
            if not self._isDisabled and not self._isSelected then
                if self.mAltToggledFlag then
                    self:SetTexture(self.mAltNormal)
                else
                    self:SetTexture(self.mNormal)
                end
                self:OnRolloverEvent('exit')
                self:Play()
                self.mMouseOver = false
            end
            eventHandled = true
        elseif event.Type == 'ButtonPress' or event.Type == 'ButtonDClick' then
        
            if not self._isDisabled and not self._isSelected then
                if self.mClickCue then
                    PlaySound(self.mClickCue)
                end
                local dragger = Dragger()
                dragger.OnRelease = function(dragger, x, y)
                    dragger:Destroy()
                    self.mDragger = nil
                    if self.mMouseOver then
                        if self.mAltToggledFlag then
                            self:SetTexture(self.mAltHighlight)
                        else
                            self:SetTexture(self.mHighlight)
                        end
                        self:OnRolloverEvent('exit')
                        if not IsDestroyed(self) then
                            self:Play()
                        end
                        self:OnClick(event.Modifiers)
                    end
                end
                dragger.OnCancel = function(dragger)
                    if self.mMouseOver then
                        if self.mAltToggledFlag then
                            self:SetTexture(self.mAltHighlight)
                        else
                            self:SetTexture(self.mHighlight)
                        end
                        self:Play()
                    end
                    dragger:Destroy()
                    self.mDragger = nil
                end
                self.mDragger = dragger
                if self.mAltToggledFlag then
                    self:SetTexture(self.mAltActive)
                else
                    self:SetTexture(self.mActive)
                end
                self:OnRolloverEvent('down')
                self:Play()
                PostDragger(self:GetRootFrame(), event.KeyCode, dragger)         

            else
                if self.mFailToActivateCue then
                    PlaySound(self.mFailToActivateCue)
                end
            end
            eventHandled = true
        end

        return eventHandled
    end,

    OnClick = function(self, modifiers) end

}
